import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/templates/home/index'
import Login from '@/templates/login/index'
import NotifyPsw from '@/templates/home/notifyPsw'
//系统管理
import ArticleIndex from '@/templates/article/index'
import OnlineSurveyIndex from '@/templates/onlineSurvey/index'
import SurveyAdd from '@/templates/onlineSurvey/survey_add'
import OnlineResult from '@/templates/onlineSurvey/online_result'
import ResultDetail from '@/templates/onlineSurvey/result_detail'
import BannerIndex from '@/templates/banner/index'
import UserIndex from '@/templates/user/index'

Vue.use(Router)

const homeChildren = [{
        path: '/home/articleIndex',
        name: 'articleIndex',
        component: ArticleIndex,
    },
    {
        path: '/home/onlineIndex',
        name: 'onlineIndex',
        component: OnlineSurveyIndex
    },
    {
        path: '/home/surveyAdd',
        name: 'surveyAdd',
        component: SurveyAdd
    },
    {
        path: '/home/onlineResult',
        name:'onlineResult',
        component: OnlineResult
    },
    {
        path: '/home/resultDetail',
        name: 'resultDetail',
        component: ResultDetail
    },
    {
        path: '/home/bannerIndex',
        name: 'bannerIndex',
        component: BannerIndex
    },
    {
        path: '/home/userIndex',
        name: 'userIndex',
        component: UserIndex
    }
];
const routes = [{
        path: '/home',
        name: 'Home',
        component: Home,
        // meta: {
        //     requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        // },
        children: homeChildren
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/',
        redirect: '/login'
    },
    {
        path:'/notifyPsw',
        name: 'notifyPsw',
        component: NotifyPsw
    }
    /*    {
            path: '/return_urls',
            redirect: '/static/returnUrl.html'
        }*/
];

const router = new Router({
    /*mode:'history',*/
    routes
});

router.beforeEach((to, from, next) => {
    if (to.matched.some(r => r.meta.requireAuth)) {
        const userInfo = JSON.parse(getCookie('userInfo'));
        if (store.state.userData.token || userInfo) {
            next();
        } else {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
        }
    } else {
        next();
    }
})

export default router;
